package logic

import (
	"context"
	"errors"
	"video/model/mysql"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NickNameUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNickNameUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NickNameUpdateLogic {
	return &NickNameUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改昵称
func (l *NickNameUpdateLogic) NickNameUpdate(in *videorpc.NickNameUpdateRequest) (*videorpc.NickNameUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在")
	}

	user.NickName = in.NewNickName

	err = user.NickNameUpdate(mysql.DB)
	if err != nil {
		return nil, errors.New("昵称修改失败")
	}

	return &videorpc.NickNameUpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
